<template>
  <div class="container">
    <el-form ref="form" label-width="60px" label-position="left" :inline="true" class="bigbox">
      <div slot="header" v-if="subject.id && subject.name">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-form-item label="目录名称" label-width="80px">
        <el-input v-model="requestParams.directoryName"></el-input>
      </el-form-item>
       <el-form-item label="状态">
              <el-select v-model="requestParams.state">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
             <!-- <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item> -->
      <el-button @click="clear()">清除</el-button>
      <el-button @click="filter()" type="primary">搜索</el-button>

      <el-button type="success" icon="el-icon-edit" class="boxone" @click="openDialog()"
        >新增学科</el-button>
        <el-button class="boxone" v-if="subject.id && subject.name" type="text" icon="el-icon-back" @click="$router.back()">返回学科</el-button>

      <DirectoryAdd
        ref="directorysAdd"
        :data="currDirectory"
        @updateList="updateList()"
      ></DirectoryAdd>
    </el-form>
    <el-row>
      <el-col :span="24"
        ><div class="grid-content bg-purple-dark el-icon-info">
          数据一共{{ total }}条
        </div></el-col>
    </el-row>
    <div class="block">
      <span class="demonstration">
        <el-table :data="directorys" stripe style="width: 100%">
          <el-table-column type="index" label="序号" width="100">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="100">
          </el-table-column>
          <el-table-column prop="directoryName" label="目录名称" width="140">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column label="创建日期" width="160px">
            <template slot-scope="scope">{{
              scope.row.addDate | parseTimeByString
            }}</template>
          </el-table-column>
          <el-table-column prop="totals" label="面试题数量"> </el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">{{
              scope.row.state === 1 ? "已启用" : "已禁用"
            }}</template>
          </el-table-column>
          <el-table-column  label="操作" width="300">
            <template slot-scope="scope">
              <el-button type="text" @click="toggleState(scope.row)">{{scope.row.state===1?'禁用':'启用'}}</el-button>
              <el-button type="text" @click="openDialog(scope.row)" :disabled="scope.row.state===1">修改</el-button>
              <el-button type="text" @click="delDirectory(scope.row)" :disabled="scope.row.state===1||scope.row.totals > 0">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </span>
      <el-pagination
        style="margin-top: 20px; text-align: right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        @current-change="pager"
        :page-sizes="[5, 10, 20, 50]"
        @size-change="handleSizeChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/directorys.js'
import DirectoryAdd from '../components/directorys-add.vue'
export default {
  components: { DirectoryAdd },
  created () {
    this.getList()
  },
  data () {
    return {
      requestParams: {
        directoryName: '',
        state: null,
        page: 1,
        pagesize: 10
      },
      directorys: [],
      total: 0,
      currDirectory: {}
    }
  },
  methods: {
    async delDirectory (subject) {
      await this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(subject)
      this.$message.success('删除成功')
      this.getList()
    },
    async toggleState (directory) {
      await changeState({
        id: directory.id,
        state: directory.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      directory.state = directory.state === 1 ? 0 : 1
    },
    updateList () {
      if (!this.currDirectory.id) {
        this.requestParams.page = 1
      }
      this.getList()
      console.log(123)
    },
    clear () {
      this.requestParams = {
        directoryName: null,
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    filter () {
      this.requestParams.page = 1
      this.getList()
    },
    openDialog (directory = {}) {
      this.currDirectory = directory
      this.$nextTick(() => {
        this.$refs.directorysAdd.open()
      })
    },
    handleSizeChange (size) {
      this.requestParams.page = 1
      this.requestParams.pagesize = size
      this.getList()
    },
    // 切换分页
    pager (np) {
      this.requestParams.page = np
      this.getList()
    },
    async getList () {
      const { data } = await list(this.requestParams)
      console.log(data)
      this.directorys = data.items
      this.total = data.counts
    }
  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  }
}
</script>

<style scoped lang='scss'>
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
  width: 100%;
  line-height: 36px;
  padding-left: 5px;
}
.boxone {
  float: right;
}
.container {
  margin: 2px 5px 0 5px;
  background-color: #fff;
  padding-top: 5px;
}
</style>
